package com.cmccsi.service.impl;

import net.sf.json.JSONObject;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.log4j.Logger;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.exceptions.JedisConnectionException;

import com.cmccsi.entity.qxdeptDto;
import com.cmccsi.mapper.qxdeptDtoMapper;
import com.cmccsi.service.DeptService;
import com.cmccsi.utils.MybatisUtil;
import com.cmccsi.utils.RedisConnPool;
import com.cmccsi.utils.RedisKeyProperties;

public class DeptRedisServiceImpl implements DeptService {

	private static final Logger logger = Logger
			.getLogger(DeptRedisServiceImpl.class);

	@Override
	public Boolean delete(Long id) {
		Jedis sc = null;
		boolean broken = false;
		try {
			sc = RedisConnPool.getJedis();
			// {"id":1,"optype":"insert"}
			JSONObject json = new JSONObject();
			json.put("table", "dept");
			json.put("id", id);
			json.put("optype", "delete");
			sc.lpush(RedisKeyProperties.sync_db, json.toString());
			return true;
		} catch (JedisConnectionException e) {
			logger.error(e.getMessage(), e);
			broken = true;
		} finally {
			RedisConnPool.returnJedis(sc, broken);
		}
		return false;
	}

	@Override
	public Boolean update(qxdeptDto dept) {
		// TODO Auto-generated method stub
		Jedis sc = null;
		boolean broken = false;
		try {
			sc = RedisConnPool.getJedis();
			// {"id":1,"optype":"insert"}
			JSONObject json = new JSONObject();
			json.put("table", "dept");
			json.put("id", dept.getId());
			json.put("optype", "update");
			sc.lpush(RedisKeyProperties.sync_db, json.toString());
			return true;
		} catch (JedisConnectionException e) {
			logger.error(e.getMessage(), e);
			broken = true;
		} finally {
			RedisConnPool.returnJedis(sc, broken);
		}
		return false;
	}

	@Override
	public qxdeptDto find(Long deptId) {
		// TODO Auto-generated method stub

		return null;
	}

	@Override
	public Boolean save(qxdeptDto dept) {
		// TODO Auto-generated method stub
		Jedis sc = null;
		boolean broken = false;
		try {
			sc = RedisConnPool.getJedis();
			// {"id":1,"optype":"insert"}
			JSONObject json = new JSONObject();
			json.put("table", "dept");
			json.put("id", dept.getId());

			json.put("optype", "insert");

			sc.lpush(RedisKeyProperties.sync_db, json.toString());
			return true;
		} catch (JedisConnectionException e) {
			logger.error(e.getMessage(), e);
			broken = true;
		} finally {
			RedisConnPool.returnJedis(sc, broken);
		}
		return false;
	}

}
